package com.silvestre.conta.dinheiro;

import java.text.DecimalFormat;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ContaDinheiroActivity extends Activity {

    private EditText edNota2, edNota5, edNota10, edNota20, edNota50, edNota100;
    private EditText edMoeda1c, edMoeda5, edMoeda10, edMoeda25, edMoeda50, edMoeda1;
    double total;
    static final int MENU_SOBRE = 1;
    private Activity instance;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        instance = this;
        
        getIds();

        Button btCalc = (Button) findViewById(R.id.btCalc);
        btCalc.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                total = calcularTotal();
                DecimalFormat decimalFormat = new DecimalFormat();
                decimalFormat.setMinimumFractionDigits(2);

                AlertDialog.Builder dialog = new AlertDialog.Builder(ContaDinheiroActivity.this);
                dialog.setTitle("Resultado");
                dialog.setMessage("Valor Total R$ " + decimalFormat.format(total));
                dialog.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Toast.makeText(AndroidDialogActivity.this, "Ok", Toast.LENGTH_SHORT).show();
                    }
                });

                dialog.show();
                // Toast.makeText(getBaseContext(), "Total R$ " + decimalFormat.format(total), Toast.LENGTH_LONG).show();

            }
        });

    }

    private void getIds() {

        edNota2 = (EditText) findViewById(R.id.edndois);
        edNota5 = (EditText) findViewById(R.id.edncinco);
        edNota10 = (EditText) findViewById(R.id.edndez);
        edNota20 = (EditText) findViewById(R.id.ednvinte);
        edNota50 = (EditText) findViewById(R.id.edncinque);
        edNota100 = (EditText) findViewById(R.id.edncem);

        edMoeda1c = (EditText) findViewById(R.id.edmumc);
        edMoeda5 = (EditText) findViewById(R.id.edmcinco);
        edMoeda10 = (EditText) findViewById(R.id.edmdez);
        edMoeda25 = (EditText) findViewById(R.id.edmvintecinco);
        edMoeda50 = (EditText) findViewById(R.id.edmcinque);
        edMoeda1 = (EditText) findViewById(R.id.edmum);

    }

    private double calcularTotal() {
        total = 0;

        total += parseCampo(edNota2, 0);
        total += parseCampo(edNota5, 1);
        total += parseCampo(edNota10, 2);
        total += parseCampo(edNota20, 3);
        total += parseCampo(edNota50, 4);
        total += parseCampo(edNota100, 5);

        total += parseCampo(edMoeda1c, 6);
        total += parseCampo(edMoeda5, 7);
        total += parseCampo(edMoeda10, 8);
        total += parseCampo(edMoeda25, 9);
        total += parseCampo(edMoeda50, 10);
        total += parseCampo(edMoeda1, 11);

        // total = Math.ceil(total);

        return total;
    }

    private double parseCampo(EditText ed, int id) {

        double valor = 0;

        if (ed.getText().toString().trim() != "") {
            try {
                valor = Double.parseDouble(ed.getText().toString().trim());

                switch (id) {
                case 0:
                    valor *= 2;
                    break;
                case 1:
                    valor *= 5;
                    break;
                case 2:
                    valor *= 10;
                    break;
                case 3:
                    valor *= 20;
                    break;
                case 4:
                    valor *= 50;
                    break;
                case 5:
                    valor *= 100;
                    break;
                case 6:
                    valor = 0.01 * valor;
                    break;
                case 7:
                    valor = 0.05 * valor;
                    break;
                case 8:
                    valor = 0.10 * valor;
                    break;
                case 9:
                    valor = 0.25 * valor;
                    break;
                case 10:
                    valor = 0.50 * valor;
                    break;
                case 11:
                    valor = 1 * valor;
                    break;

                }

            } catch (Exception e) {
                valor = 0;
            }

        }

        return valor;

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_SOBRE, 0, "Sobre").setIcon(android.R.drawable.ic_dialog_info);

        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {

        switch (item.getItemId()) {
        case MENU_SOBRE:

            Dialog dialog = new Dialog(instance);                
            dialog.setCancelable(true);
            dialog.setContentView(R.layout.dialogsobre);
            dialog.show();

            return true;

        }

        return false;
    }
}